﻿using HZY.Framework.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
using Sunny.UI;
using WinFormsSprayProcessSCADASystem.Components;

namespace WinFormsSprayProcessSCADASystem.Pages
{
    public partial class PageEquipmentMonitor : UIPage, IScopedSelfDependency

    {
        private List<Control> allControls;
        public PageEquipmentMonitor()
        {
            InitializeComponent();
            allControls = this.GetAllControls();
            Globals.ServiceProvider.GetRequiredService<MainForm>().PlcValCahnge += UpdateState;
        }

        private void UpdateState()
        {
            foreach (var item in allControls)
            {
                if (item is UserDeviceState userDeviceState)
                {
                    userDeviceState.State = Convert.ToInt32(Globals.DataDic[userDeviceState.VariableName]) == 1;
                }
                else if (item is UserAlarmState alarmState)
                {
                    alarmState.State = Convert.ToInt32(Globals.DataDic[alarmState.VariableName]) == 1;
                }
                else if (item is UserVarCurrentValue userVarCurrentValue)
                {

                    userVarCurrentValue.VarValue = (float)Convert.ToDouble(Globals.DataDic[userVarCurrentValue.VariableName]);
                }
            }
        }
    }
}
